package com.marry.requestLogger.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ml
 * @create 2017-07-03--15:33
 */
public class LoggerUtils {

    public static final String LOGGER_TETURN="_logger_return";

    public LoggerUtils() {


    }

    /**
     * 获得客户端的ip地址
     * @param request
     * @return
     */
    public static String getCilentIp(HttpServletRequest request){

        String ip=request.getHeader("x-forwarded-for");
        if(ip==null || ip.trim()==""||"unknown".equalsIgnoreCase(ip)){
            ip=request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        return ip;
    }

    /**
     * 判断是否为ajax请求
     * @param request
     * @return
     */

    public static String getRequestType(HttpServletRequest request){
        return request.getHeader("X-Request-With");
    }
}
